package com.md.base.db;

import com.md.util.ObjectUtil;
import com.md.util.StringUtil;
import lombok.Data;

import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @author 刘骄阳
 * 2025-06-14 2:41
 */
@Data
public class ExpressionParamBase {
  private String expression;
  /**
   * 列名
   */
  private String column;
  /**
   * 比较符
   */
  private String operators;
  /**
   * 值
   */
  private Object value;

  public static String parseString(final Object value) {
    if (value instanceof String) {
      return "'" + value + "'";
    } else if (value instanceof Collection) {
      return ((Collection<?>) value).stream().map(ExpressionParamBase::parseString).collect(Collectors.joining(", "));
    }
    return StringUtil.getToString(value);
  }

  public String getExpression() {
    if (ObjectUtil.isEmpty(expression)) {
      return "AND";
    }
    return expression;
  }

  public String parse() {
    if (value == null) {
      return " " + getExpression() + " " + column + " is null";
    }

    return " " + getExpression() + " " + column + " " + operators + " " + parseString(value);
  }
}
